// pages/afflatus/common/filter-item/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否单选
    isSingle:{
      type:Boolean,
      value:false
    },
    title:{
      type:String,
      value:'距离'
    },
    dataList:{
       type:Array,
       value:[
         {
           id:0,
           text:"测试1",
           selected:false
         },
         {
           id:0,
           text:"测试2",
           selected:false
         },
         {
           id:0,
           text:"测试3",
           selected:false
         },
         {
          id:0,
          text:"测试1",
          selected:false
        },
        {
          id:0,
          text:"测试2",
          selected:false
        },
        {
          id:0,
          text:"测试3",
          selected:false
        }
       ],
       observer:function(newValue){
        // this.setData({
        //   list:newValue
        //  })
       
        //  console.log(newValue)
         if(newValue.length > 6){
           if(this.data.select){
            this.setData({
              list:newValue
            })
           }else{
            this.setData({
              list:newValue.slice(0,6)
            })
           }
         }else{
           this.setData({
             list:newValue
            })
         }
        //  console.log("dataList:",this.data.dataList)
       }
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    select:false,
    list:[]
  },
  attached(){
   

  },

  /**
   * 组件的方法列表
   */
  methods: {
    arrowTap(){
       this.data.select = !this.data.select
       
       // 收起来
       if(this.data.select){
          this.data.list = this.data.dataList
       }else{
          this.data.list = this.data.dataList.slice(0, 6)
       }
       this.setData({
          select:this.data.select,
          list:this.data.list
       })
      //  console.log('item-select:',this.data.select)
      //  console.log("list:",this.data.list)
        
    },
    
    itemClcik(e){
  
       const {index,item} = e.currentTarget.dataset
       const selectItem = this.data.list[index]
       if(this.data.isSingle){
          let dataList = this.data.list.map(item => {
              item.selected = false
              return item
          })
          selectItem.selected = !selectItem.selected
          this.setData({
             list : this.data.list
          })
       }else{
          selectItem.selected = !selectItem.selected
          this.setData({
            list : this.data.list
          })
       }
       this.triggerEvent('selectItem',this.data.dataList)
    },
    handlerRes(){
       let resArr = this.data.dataList.filter(item => item.selected === true) 
      //  console.log('resArr:',resArr)
       return resArr
    }
  }
})
